package com.test.jdk.lambda;

import sun.awt.PeerEvent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * lambda表达式
 */
public class lambda {
    public static void main(String[] args) {

        //普通写法
        goshow(new IUserService() {
            @Override
            public void show() {
                System.out.println("普通方法调用了show()方法");
            }
        });

        //自定义lombad表达四
        goshow(() -> {
            System.out.println("lambad表达式调用了show()方法");
        });

        List<Persion> persionList = new ArrayList<>();
        Persion persion = new Persion(18, "杰伦", 188);
        Persion persion1 = new Persion(19, "德华", 166);
        Persion persion2 = new Persion(22, "俊杰", 177);
        Persion persion3 = new Persion(88, "富城", 189);

        persionList.add(persion);
        persionList.add(persion1);
        persionList.add(persion2);
        persionList.add(persion3);


        //排序Comparator 函数式接口
        //普通实现方式
//        Collections.sort(persionList, new Comparator<Persion>() {
//            @Override
//            public int compare(Persion o1, Persion o2) {
//                //根据身高进行排序
//                return o1.getHeight() - o2.getHeight();
//            }
//        });
//
//        for (Persion p : persionList) {
//            System.out.println(p);
//        }

        //lombda表达式实现
        System.out.println("lombda表达式实现-------------------");

        Collections.sort(persionList, (Persion o1, Persion o2) -> {
            //根据身高进行排序降序
            return o2.getHeight() - o1.getHeight();
        });
        for (Persion p : persionList) {
            System.out.println(p);
        }

    }

    private static void goshow(IUserService userService) {
        userService.show();
    }
}
